由于相册自动同步过来,都在同一个文件夹下面,这样以后会越来越多,不利于打理。
所以增加个自动分类,目前分类规则是按照拍照的定位进行分配,如果没有gps信息就放到一起。视频也是一样,有定位的就按定位,没有的就将视频放到一块。
感谢小涛的代码,代码如下:
'''
本文件用来批量根据照片属性对照片进行分类
输入文件夹
输出根据省市区进行分类的文件结构
会跳过没有经纬度的照片
'''
import requests
import exifread
import time
import os
import shutil
import sys
class GetPhotoInfo:
def __init__(self, photo):
self.photo = photo
# 百度地图ak 请替换为自己申请的ak
self.ak = 'nYPs4LQ9a4VhVxj55AD69K6zgsRy9o4z'
self.location = self.get_photo_info()
def get_photo_info(self, ):
print('当前解析文件路径:{}'.format(self.photo))
try:
with open(self.photo, 'rb') as f:
tags = exifread.process_file(f)
# 打印照片其中一些信息
# print('拍摄时间:', tags['EXIF DateTimeOriginal'])
# print('照相机制造商:', tags['Image Make'])
# print('照相机型号:', tags['Image Model'])
# print('照片尺寸:', tags['EXIF ExifImageWidth'], tags['EXIF ExifImageLength'])
# 纬度
lat_ref = tags["GPS GPSLatitudeRef"].printable
lat = tags["GPS GPSLatitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
if len(lat) < 4:
return None
lat = float(lat[0]) + float(lat[1]) / 60 + float(lat[2]) / float(lat[3]) / 3600
if lat_ref != "N":
lat = lat * (-1)
# 经度
lon_ref = tags["GPS GPSLongitudeRef"].printable
lon = tags["GPS GPSLongitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
if len(lon) < 4:
return None
lon = float(lon[0]) + float(lon[1]) / 60 + float(lon[2]) / float(lon[3]) / 3600
if lon_ref != "E":
lon = lon * (-1)
except KeyError:
# print("ERROR:请确保照片包含经纬度等EXIF信息。\n")
return None
else:
# print("经纬度:", lat, lon)
return lat, lon
def get_location(self):
if self.location is None:
return
url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak={}&output=json' \
'&coordtype=wgs84ll&location={},{}'.format(self.ak, *self.location)
response = requests.get(url).json()
status = response['status']
if status == 0:
address = response['result']['formatted_address']
print('详细地址:', address)
else:
print('baidu_map error')
provinceidx = address.find("省")
province = "未知"
if provinceidx > 0:
province = address[0:provinceidx+1]
address = address[provinceidx+1:]
city = "未知"
cityidx = address.find("市")
if cityidx > 0:
city = address[0:cityidx+1]
address = address[cityidx+1:]
area = "未知"
areaidx = address.find("区")
if areaidx > 0:
area = address[0: areaidx + 1]
address = address[areaidx + 1:]
else:
areaidx = address.find("县")
if areaidx > 0:
area = address[0:areaidx + 1]
address = address[areaidx + 1:]
else:
areaidx = address.find("市")
if areaidx > 0:
area = address[0: areaidx + 1]
address = address[areaidx + 1:]
detail = address
decompose = [province, city, area, detail] # 分解后的地址
return decompose
def get_all_path(file_path):
all_file_name = []
for maindir, subdir, file_name_list in os.walk(file_path):
for filename in file_name_list:
# 先保存到一起,如果mp4可以正常解析就直接使用,如果不行再移动到视频目录
if filename.endswith("jpg") or filename.endswith("mp4"):
file = []
apath = os.path.join(maindir, filename)
file.append(apath)
file.append(filename)
all_file_name.append(file)
return all_file_name
# dst必须传全路径
def movefile(src,dst):
# dst是全路径,如果已经存在则直接删除src,跳过此文件
if os.path.exists(dst):
os.remove(src)
print(dst + "已经存在")
else:
shutil.move(src, dst)
if __name__ == '__main__':
print("开始处理")
if len(sys.argv) > 2:
inpupath = sys.argv[1]
outputpath = sys.argv[2]
if inpupath is None or len(inpupath) <= 0:
inpupath = r"E:\项目资料中心\相册整理\未分类"
if outputpath is None or len(inpupath) <= 0:
outputpath = r"E:\项目资料中心\相册整理\已分类"
else:
inpupath = r"E:\项目资料中心\相册整理\未分类"
outputpath = r"E:\项目资料中心\相册整理\已分类"
if not os.path.exists(outputpath):
print('根目录不存在,创建')
os.mkdir(outputpath)
all_file_name = get_all_path(inpupath)
for i in all_file_name:
filePathName = i[0]
filename = i[1]
try:
decompose = GetPhotoInfo(filePathName).get_location()
except:
continue
else:
a = 1
#如果解析失败,则直接移动到文件夹
if decompose is None:
#如果无法解析并且是mp4格式的就直接移动到视频
if filename.endswith("mp4"):
movefile(filePathName, outputpath + r"\\手机自动同步\\视频\\" + filename);
else:
#无法解析的先放到手机自动同步里
movefile(filePathName, outputpath + r"\\手机自动同步\\" + filename);
continue
add_sheng = outputpath + '\\' + decompose[0]
add_shi = add_sheng + '\\' + decompose[1]
add_qu = add_shi + '\\' + decompose[2]
add_detail = add_qu + '\\' + decompose[3]
if not os.path.exists(add_detail):
os.makedirs(add_detail)
movefile(filePathName, add_detail + '\\' + filename)
time.sleep(0.1)
print("处理完成");
执行一下看看。

效果还可以,然后给打包成exe
定时执行,自动分类
打开任务计划,创建基本任务。

每天

凌晨



填写执行脚本位置,参数要加上。

然后完成

这样就会每天凌晨自动同步了。
但是还有一个问题,同步到nas上后,nas还需要点击库,开始,才会索引照片。
增加一个自动索引
首先输入
/usr/bin/docker container ls

然后记录id
执行/usr/bin/docker exec -it id /photoprism/bin/photoprism index

可以看到,已经开始同步了,说明没问题。
下面就是给增加到定时执行。
由于经常可能会修改东西,所以直接写到一个shell中,后面想怎么改就怎么改。




经过测试这个想法没问题,但是docker重启就是丢失配置,所以需要给增加到go里面

echo "0 4 * * * /mnt/user/mx-data/shell/autoupdate.sh &> /mnt/user/mx-data/shell/autoupdate.result" >> /etc/cron.d/root
这样的话,手机自动同步到nas,凌晨3点电脑开始将同步的照片进行分类,然后4点相册开始自动索引。完美
Comments | NOTHING